function ajax(obj) {
    //创建ajax的对象
    let xhr = new XMLHttpRequest();

    //初始化参数
    let params = '';
    if (obj.data != '') {
        for (key in obj.data) {
            params += `${key}=${obj.data[key]}&`;
        }
        params = params.substring(0, params.length - 1);
    }

    //接收传递类型并转为小写
    let type = obj.type.toLowerCase();

    xhr.open(type, type == 'get' ? `${obj.url}?${params}` : obj.url);
    // post  - 设置请求头
    if (type === 'post') {
        xhr.setRequestHeader('Content-Type', obj.contentType  ? obj.contentType : 'application/x-www-form-urlencoded');
    }

    type == 'get' ? xhr.send() : xhr.send(obj.contentType == 'application/json;charset=UTF-8' ? JSON.stringify(obj.data) : params);

    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
            let result = xhr.responseText;
            obj.success(result)
        }
    }
}
